<?php

use Biz\model\brandActivity\UserAddressModel;
use Biz\model\NewProduct\NewProductWikiModel;
use Biz\model\NewProduct\NewProductWinnerListModel;
use Biz\module\Classes\wiki\Infos;
use Biz\service\NewProduct\NewProductService;

/**
 * Description: 我喜欢的
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 14:33
 */
class MyLikeListAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        //每页多少数据
        $per_page = (int)$this->getRequest()->getPost('per_page', 10);
        //当前页
        $page = (int)$this->getRequest()->getPost('page', 1);
        //获取wiki_id集合
        $uid = $this->uid;
        if (!$uid){
            throw new \Exception("请登录重试！");
        }
        $current = $page * $per_page;
        $list['total'] = 0;
        $list['per_page'] = $per_page;
        $list['current_page'] = $page;
        $list['list'] = [];
        $nids = NewProductService::getUserLikeList($uid, 'nid');
        $count = 0;
        if ($nids) {
            $where['id'] = $nids;
            $wiki_ids = NewProductWikiModel::all($where, 'wiki_id');
            //总条数
            $count = count($wiki_ids);
            //偏移量，当前页-1乘以每页显示条数
            $start = ($page - 1) * $per_page;
            //每页的wiki_id集合
            $wiki_ids_page = array_slice($wiki_ids, $start, $per_page);
            $list['total'] = $count;
            $wiki_list = [];
            if ($wiki_ids_page) {
                $wiki_list = Infos::infos($wiki_ids_page, Infos::FIELDS_GROUP_NEW_PRODUCT_MY_LIKE, $uid);
            }
            $list['list'] = $wiki_list ? array_values($wiki_list) : [];
        }

        $list['is_over'] = ($count <= $current) ? 1 : 0;
        $this->setSuccess($list);

    }
}